/*
 * @Author: your name
 * @Date: 2021-11-29 11:03:07
 * @LastEditTime: 2021-11-29 11:06:01
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \Quality-check\src\utils\urlDownLoad.js
 */
//url下载文件
export async function downFile(url, name) {
	let imgs = ['jpg', 'png', 'jpeg', 'gif']
	let fileType = name.split('.')[1]
	if (imgs.includes(fileType)) {
		uni.previewImage({
			urls: [url]
		})
	} else {
		uni.downloadFile({
			url: url,
			success: function(res) {
				var filePath = res.tempFilePath;
				//打开文件有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
				uni.openDocument({
					filePath: escape(filePath),
					success: function(res) {
						uni.hideLoading();
					},
					fail() {
						uni.showToast({
							title: '暂不支持此类型',
							duration: 2000
						});
						uni.hideLoading();
					}
				});



			},
			fail: () => {
				uni.showToast({
					title: '文件下载失败',
					duration: 2000
				});
			}
		});
	}
	
	// let response = await fetch(url) // 内容转变成blob地址
	// let blob = await response.blob() // 创建隐藏的可下载链接
	// let objectUrl = window.URL.createObjectURL(blob)
	// let a = document.createElement('a')
	// a.href = objectUrl
	// a.download = name
	// a.click()
	// a.remove()
}
